package com.sdry.web.controller.lz;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * @ClassName: AttachmentUpload
 * @Description:文件和图片上传
 * @Author lz
 * @Date 2018年12月25日 09:41:47
 * @version 1.0
 */
@Controller
@RequestMapping("/upload")
public class AttachmentUpload {
	
	public static final int SIZE = 1024;
	// public static final long NAME = System.currentTimeMillis();
	/**
	 * 图片上传
	 * @param file 文件
	 * @param response 响应
	 * @param in 输入流
	 * @param request 请求
	 * @throws IOException IO流异常
	 */
	@RequestMapping("/picUpload")
	public void picUpload(@RequestParam(value = "file", required = false) MultipartFile[] file, HttpServletResponse response, InputStream in,
			HttpServletRequest request) throws IOException {
		response.setCharacterEncoding("UTF-8");
		String timeStamp1 = String.valueOf(System.currentTimeMillis());
		StringBuffer buffer1 = new StringBuffer();
		int count = 0;
		//图片上传的路径
		//String dir = request.getServletContext().getRealPath("/") ;
		String dir = getValue("IMG_UPLOAD") + "//simplification//" ;
		File existsFile = new File(dir);
		if (!existsFile.exists()) {
			existsFile.mkdirs();
	    }
		for (MultipartFile mFile : file) {
			count ++;
			// 图片名称
			String filename = mFile.getOriginalFilename();
			InputStream is = mFile.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(is, SIZE);
			OutputStream os = new FileOutputStream(dir + timeStamp1 + filename);
			BufferedOutputStream bos = new BufferedOutputStream(os, SIZE);
			byte[] b = new byte[1024];
			int len;
			while ((len = bis.read(b)) != -1) {
				bos.write(b, 0, len);
			}
			bis.close();
			is.close();
			bos.close();
			os.close();
			// 在数据库存入的路径
			String DBpath;
			if (count == 1) {
				DBpath = timeStamp1 + filename;
			} else {
				DBpath = "," + "//" + timeStamp1 + filename;
			}
			buffer1.append(DBpath);
		}
		List<String> list = new ArrayList<String>();
		list.add(buffer1.toString());
		System.out.println(buffer1.toString());
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(list);
		for (Object jsonObj1 : jsonArray) {
			response.getWriter().print(jsonObj1);
		}
	}
	
	/**
	 * 文件上传
	 * 
	 * @param file 文件
	 * @param response 响应
	 * @param in 输入流
	 * @param request 请求
	 */
	@RequestMapping("/filesUpload")
	public void filesUpload(@RequestParam("files") MultipartFile[] file, HttpServletResponse response, InputStream in,
			HttpServletRequest request) throws IOException {
		response.setCharacterEncoding("UTF-8");
		String timeStamp2 = String.valueOf(System.currentTimeMillis());
		StringBuffer buffer2 = new StringBuffer();
		int count = 0;
		// 文件上传的路径
		// String dir = request.getServletContext().getRealPath("/");
		String dir = getValue("IMG_UPLOAD") + "\\simplification\\" ;
		File existsFile = new File(dir);
		if (!existsFile.exists()) {
			existsFile.mkdirs();
	    }
		for (MultipartFile mFile : file) {
			count ++;
			// 文件名称
			String filename = mFile.getOriginalFilename();
			InputStream is = mFile.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(is, SIZE);
			OutputStream os = new FileOutputStream(dir + timeStamp2 + filename);
			BufferedOutputStream bos = new BufferedOutputStream(os, SIZE);
			byte[] b = new byte[1024];
			int len;
			while ((len = bis.read(b)) != -1) {
				bos.write(b, 0, len);
			}
			is.close();
			bis.close();
			os.close();
			bos.close();
			String DBpath;
			if (count == 1) {
				DBpath = "//simplification//" + timeStamp2 + filename;
			} else {
				DBpath = "," + "//simplification//" + timeStamp2 + filename;
			}
			buffer2.append(DBpath);
		}
		List<String> list = new ArrayList<String>();
		list.add(buffer2.toString());
		System.out.println(buffer2.toString());
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(list);
		for (Object jsonObj2 : jsonArray) {
			response.getWriter().print(jsonObj2);
		}
	}
	
	/**
	 * 获取项目根目录
	 * @param key
	 * @return
	 */
	public static String getValue(String key){
		Properties prop = new Properties();
		try {
			InputStream inputStream = new AttachmentUpload().getClass().getClassLoader()
					.getResourceAsStream("pathconfig.properties");
			//装载配置文件
			prop.load(inputStream);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		//返回获取的值
		return prop.getProperty(key);
	}
}
